﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using SmartRfMng.appcode;
using System.IO;
using System.Reflection;

namespace SmartRfMng
{
    public partial class frmLogin : Form
    {
       
        bool bIsLogin = false; //是否登陆
        bool bCanDoNext = true;

        //软键盘
        [DllImport("coredll.dll")]
        public extern static void SipShowIM(uint isFlag);

        public frmLogin()
        {
            InitializeComponent();
            bCanDoNext = true;
            SipShowIM(0x01);//开启软键盘
            txtuser.Text = ZT_INI.GetINI("system", "UserCode", "");
            txtpasswd.Text = ZT_INI.GetINI("system", "Passwd", "");
            if (txtuser.Text != "")
            {
                txtpasswd.Focus();
            }
        }

        private void btnset_Click(object sender, EventArgs e)
        {
            //userlogin
            //string sUserCode = txtuser.Text.Trim();
            //if (sUserCode == "")
            //{
            //    MessageBox.Show("请填写用户名！", "提示", MessageBoxButtons.OK,
            //      MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            //    return;
            //}
            //string sPassWord = txtpasswd.Text.Trim();
            //Int64 iUserId = -1;
            //string sUserType = string.Empty;
            //string sRtnMsg = string.Empty;
            //string sUserId = "-1";

            //StoreRf rf = new StoreRf();
            //bIsLogin = rf.UserLogin(sUserCode, sPassWord, out sUserId, out sUserType, out sRtnMsg);
            //if (!bIsLogin)
            //{
            //    MessageBox.Show("用户登录失败！", "提示", MessageBoxButtons.OK, 
            //      MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            //    return;
            //}

            //if (sUserType != "1")
            //{
            //    MessageBox.Show("请使用超级用户登录！", "提示", MessageBoxButtons.OK,
            //      MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            //    return;
            //}

            frmSet frmset = new frmSet();
            try
            {         
                frmset.ShowDialog();
            }
            finally
            {
                frmset = null;
            }
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            Rectangle rectangle = Screen.PrimaryScreen.Bounds;
            Win32.SetFullScreen(false, ref rectangle);//false为恢复状态栏
            SipShowIM(0x00);//关闭软键盘
            this.Close();
        }

        private void btnlogin_Click(object sender, EventArgs e)
        {

            //userlogin
            string sUserCode = txtuser.Text.Trim();
            if (sUserCode == "")
            {
                MessageBox.Show("请填写用户名！", "提示", MessageBoxButtons.OK,
                  MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                return;
            }


            if (!bCanDoNext)
            {
                return;
            }
            bCanDoNext = false;

            try
            {
                string sPassWord = txtpasswd.Text.Trim();
                Int64 iUserId = -1; 
                string sUserName = string.Empty;
                string sUserType = string.Empty;
                string sRtnMsg = string.Empty;
                string sUserId = "-1";

                StoreRf rf = new StoreRf();
                bIsLogin = rf.UserLogin(sUserCode, sPassWord, out sUserId, out sUserName, out sUserType, out sRtnMsg);
                if (!bIsLogin)
                {
                    MessageBox.Show("用户登录失败！", "提示", MessageBoxButtons.OK,
                      MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                    return;
                }

                //记录INI
                if (cbAutoLg.Checked)
                {
                    ZT_INI.PutINI("system", "UserCode", sUserCode);
                    ZT_INI.PutINI("system", "Passwd", sPassWord);
                }

                iUserId = Int64.Parse(sUserId);
                SmartVar.UserID = iUserId;
                SmartVar.UserCode = sUserCode;
                SmartVar.UserName = sUserName;
                SmartVar.UserType = sUserType; //0-普通用户 1-超级用户
                //取MAC
                SysInfo sysinfo = new SysInfo();
                SmartVar.RFNO = sysinfo.GetMac();

                //取组织号
                SmartVar.OrgCode = ZT_INI.GetINI("system", "OrgCode", "");

                //读配置文件
                SmartVar.PrinterName = ZT_INI.GetINI("system", "PrnName", "MPT-II");

                //取plul文件名
                string CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
                string FileName = CurrentPath + "\\PluFile.xml";
                SmartVar.PluFile = FileName;

                FileName = CurrentPath + "\\EtpFile.xml";
                SmartVar.EtpFile = FileName;

                FileName = CurrentPath + "\\EmpFile.xml";
                SmartVar.EmpFile = FileName;

                FileName = CurrentPath + "\\TruckFile.xml";
                SmartVar.TruckFile = FileName;

                frmMain frmmain = new frmMain();
                try
                {
                    frmmain.ShowDialog();
                }
                finally
                {
                    frmmain = null;
                    Rectangle rectangle = Screen.PrimaryScreen.Bounds;
                    Win32.SetFullScreen(false, ref rectangle);//false为恢复状态栏
                    Application.Exit();
                }
            }
            finally
            {
                bCanDoNext = true;
            }
        }

        private void txtpasswd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                btnlogin_Click(sender, e);
            }
        }

        private void txtuser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                txtpasswd.Focus();
            }
        }

        private void txtpasswd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                btnlogin_Click(sender, e);
            }
        }
    }
}